package cn.edu.zxj.springblog.service;

import cn.edu.zxj.springblog.mapper.BlogInfoMapper;
import cn.edu.zxj.springblog.mapper.UserInfoMapper;
import cn.edu.zxj.springblog.model.BlogInfo;
import cn.edu.zxj.springblog.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 * Description：
 *
 * @author: zxj
 * @date: 2024-02-04
 * @time: 20:09:50
 */
@Service
@Slf4j
public class UserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private BlogInfoMapper blogInfoMapper;

    public UserInfo selectByUsername(String username) {
        try {
            return userInfoMapper.selectByName(username);
        } catch (Exception e) {
            log.error("通过用户名查询用户信息出现错误, e: {}", e);
        }
        return null;
    }

    public UserInfo selectById(Integer id) {
        try {
            return userInfoMapper.selectById(id);
        } catch (Exception e) {
            log.error("通过 id 查询用户信息出现错误, e: {}", e);
        }
        return null;
    }

    public UserInfo getAuthorInfo(Integer blogId) {
        try {
            BlogInfo blogInfo = blogInfoMapper.selectById(blogId);
            Integer userId = blogInfo.getUserId();
            return userInfoMapper.selectById(userId);
        } catch (Exception e) {
            log.error("依据博客Id获取作者信息, 查询数据库出现错误, e: {}", e);
        }

        return null;
    }
}
